﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic.ApplicationServices;
using System.Windows.Forms;

namespace RFIDStockManagement.Common.Public
{
    public class SingleAppRunWapper: WindowsFormsApplicationBase
    {
        private Form thisForm;
        private ApplicationContext thisApp;
        private bool isForm;

        public SingleAppRunWapper(Form startForm)
        {
            thisForm = startForm;
            isForm = true;
            this.IsSingleInstance = true;
        }

        public SingleAppRunWapper(ApplicationContext curApp)
        {
            thisApp = curApp;
            isForm = false;
            this.IsSingleInstance = true;
        }

        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {
            if (isForm)
            {
                Application.Run(thisForm);
            }
            else
            {
                Application.Run(thisApp);
            }

            return false;
        }

        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            //base.OnStartupNextInstance(eventArgs);
            if (isForm)
            {
                if (thisForm.WindowState == FormWindowState.Minimized)
                {
                   
                    thisForm.Show();
                }

                thisForm.Activate();
            }
            else
            {
                //thisApp.MainForm.Show();
                thisApp.MainForm.Activate();
            }
            
        }

        /// <summary>
        /// 启动应用程序
        /// </summary>
        public void AppRun()
        {
            this.Run(new string[] { });
        }

    }
}
